/**
 * 
 */
package de.cbf.cam.dsa.inventory.composites;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.cbf.cam.dsa.model.ModelBase;
import de.cbf.cam.dsa.model.entities.OneNPC;
import de.cbf.cam.dsa.model.entities.WeaponStyle;
import de.cbf.cam.dsa.model.entities.maneuvers.ManeuverDesc;
import de.cbf.cam.dsa.model.entities.weapon.WeaponInUse;
import de.cbf.cam.dsa.model.entities.weapon.WeaponMeleeInUse;

/**
 * @author Nebelritter
 *
 */
public class CompositeWeaponTalentDisplay extends Composite {
	
	private List<ActionListener> talentSelectionListener = new ArrayList<ActionListener>();
	
	private Combo comboTalentChoice;
	private Text textManeuvers;

	private Label labelAT;
	private Label labelPA;	
//========================
//		constructors
//========================

	private Label labelFixAT;

	private Label labelFixPA;

	private Label labelFixManeuvers;


//========================
//	   public methods
//========================

	/**
	 * @param parent
	 * @param style
	 */
	public CompositeWeaponTalentDisplay(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(4,false);
		layout.marginHeight = 0;
		layout.marginWidth  = 0;
		layout.horizontalSpacing = 2;
		layout.verticalSpacing   = 0;
		setLayout(layout);	
		
//		setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
		
		comboTalentChoice = new Combo(this,SWT.NORMAL|SWT.READ_ONLY);
		comboTalentChoice.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,4,1));
		
		labelFixAT = new Label(this, SWT.LEFT); 
		labelFixAT.setLayoutData(new GridData(30,15));
		labelFixAT.setText("AT: ");
		
		labelAT = new Label(this, SWT.CENTER); 
		labelAT.setLayoutData(new GridData(30,15));		
		
		labelFixPA = new Label(this, SWT.LEFT); 
		labelFixPA.setLayoutData(new GridData(30,15));
		labelFixPA.setText("PA: ");
		
		labelPA = new Label(this, SWT.CENTER); 
		labelPA.setLayoutData(new GridData(30,15));
		
		labelFixManeuvers = new Label(this, SWT.LEFT); 
		GridData gridData = new GridData(SWT.FILL,SWT.CENTER,true,false,4,1);
		gridData.heightHint = 15;
		labelFixManeuvers.setLayoutData(gridData);
		labelFixManeuvers.setText("Derzeitige Maneuver:");
		
		textManeuvers = new Text(this,SWT.BORDER|SWT.WRAP|SWT.V_SCROLL|SWT.MULTI|SWT.READ_ONLY);
		GridData textData = new GridData(SWT.FILL,SWT.FILL,true,true,4,2);
		textData.heightHint = 45;
		textData.widthHint 	= 90;
		textManeuvers.setLayoutData(textData);
		textManeuvers.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
	}

	public WeaponStyle getSelectedStyle(){
		int index 			= comboTalentChoice.getSelectionIndex();
		String selection 	= comboTalentChoice.getItem(index);
		Object data 		= comboTalentChoice.getData(selection);
		return (WeaponStyle) data;
	}
	
	public boolean setFocus(){
		comboTalentChoice.setFocus();
		return true;
	}
	
	public void setData(OneNPC character,WeaponInUse weapon) {	
		if(weapon!=null){
			List<ManeuverDesc> maneuvers = ModelBase.getInstance().getPossibleManeuversArmedCharacter(character);
			String maneuverDesc="";
			for(int i=0;i<maneuvers.size();i++){
				maneuverDesc+=maneuvers.get(i).getManeuverName();
				if(i<maneuvers.size()-1){
					maneuverDesc+=", ";
				}
			}				
			textManeuvers.setText(maneuverDesc);					
		}		
		
		if (weapon instanceof WeaponMeleeInUse) {
			WeaponMeleeInUse weaponMeleeInUse = (WeaponMeleeInUse) weapon;
			List<WeaponStyle> styles = weaponMeleeInUse.getWeapon().getStyles();
			WeaponStyle activeStyle = weapon.getActiveStyle(); 
			comboTalentChoice.removeAll();
			for(int i=0;i<styles.size();i++){
				WeaponStyle style = styles.get(i);
				comboTalentChoice.add(style.getTalentName());
				comboTalentChoice.setData(style.getTalentName(),style);
				if(style.getTalentName().equals(activeStyle.getTalentName())){
					comboTalentChoice.select(i);
				}
			}
			
			labelAT.setText(weaponMeleeInUse.getATtack()+"");
			labelPA.setText(weaponMeleeInUse.getPArade()+"");
		}		
		layout();
	}

	

	public void disable() {
		labelFixAT.setEnabled(false);
		labelFixPA.setEnabled(false);
		labelFixManeuvers.setEnabled(false);
		comboTalentChoice.setEnabled(false);
		textManeuvers.setEnabled(false);
		textManeuvers.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		labelAT.setEnabled(false);
		labelPA.setEnabled(false);		
	}

	public void enable() {
		labelFixAT.setEnabled(true);
		labelFixPA.setEnabled(true);
		labelFixManeuvers.setEnabled(true);
		comboTalentChoice.setEnabled(true);
		textManeuvers.setEnabled(true);
		textManeuvers.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		labelAT.setEnabled(true);
		labelPA.setEnabled(true);	
	}
	
	public void setTalentChoiceEnabled(boolean enabled){
		comboTalentChoice.setEnabled(enabled);
	}
//========================
//		listeners
//========================
	public boolean registerTalentSelectionListener(ActionListener listener){
		return talentSelectionListener.add(listener);
	}
	public boolean deRegisterTalentSelectionListener(ActionListener listener){
		return talentSelectionListener.remove(listener);
	}
	class TalentSelection extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event= new ActionEvent(CompositeWeaponTalentDisplay.this,ActionEvent.ACTION_PERFORMED, "talentSelected");
			for (ActionListener listener : talentSelectionListener) {
				listener.actionPerformed(event);
			}
		}		
	}

	
}
